// 选择排序
// 找到当前最大值,和最后一位交换
// 再对剩下的数组进行相同操作
function selection (list) {
  for (let i = list.length - 1; i > 0; i--) {
    let pos = findMaxPos(list, i);
    [list[pos], list[i]] = [list[i], list[pos]]
  }
  return list
}

function findMaxPos (list, len) {
  let max = -Infinity
  let pos = 0
  for (let i = 0; i <= len; i++) {
    const item = list[i];
    if (item > max) {
      max = item
      pos = i
    }
  }
  return pos
}

let list = [5,6,4,3,7,9,5,1,2]
console.log(selection(list))